/*  GMooc - A Java web application for delivering massive open online courses
*   Copyright (C) 2012 Charles A. Wight
*   
*    This program is free software: you can redistribute it and/or modify
*   it under the terms of the GNU General Public License as published by
*   the Free Software Foundation, either version 3 of the License, or
*   (at your option) any later version.
*
*   This program is distributed in the hope that it will be useful,
*   but WITHOUT ANY WARRANTY; without even the implied warranty of
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*   GNU General Public License for more details.
*
*   You should have received a copy of the GNU General Public License
*   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package org.gmooc;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Id;

import com.googlecode.objectify.Key;
import com.googlecode.objectify.annotation.Cached;
import com.googlecode.objectify.annotation.Unindexed;

@Cached
public class Assignment implements Comparable<Assignment>,Serializable {
	private static final long serialVersionUID = 137L;
	@Unindexed	@Id Long id;
	String assignmentType;
	String title;
	String instructions;
	List<Long> topicIds;
	Date deadline;
	int possiblePoints;
	List<String> resourceLinkIds = new ArrayList<String>();
	List<Key<Question>> questionKeys = new ArrayList<Key<Question>>();

    Assignment() {}
    
    Assignment(String assignmentType,String title) {
    	this.assignmentType = assignmentType;
    	this.title = title;
    }

    String getAssignmentType() {
    	return assignmentType;
    }
    
    void setAssignmentType(String a) {
    	assignmentType = a;
    }
    
    String getTitle() {
    	return title==null?"":title;
    }
    
    void setTitle(String s) {
    	title = s==null?"":s;
    }
    
    String getInstructions() {
    	return instructions;
    }
    
    void setInstructions(String s) {
    	instructions = s;
    }
    
    long getTopicId() {
    	return topicIds.get(0);  // returns the first topicId in the list
    }

    void addTopicId(long t) {
    	topicIds.add(t);
    }

    boolean removeTopicId(long t) {
    	return topicIds.remove(t);
    }
    
    Date getDeadline() {
    	return deadline;
    }
    
    void setDeadline(Date d) {
    	deadline = d;
    }

    int getPossiblePoints() {
    	return possiblePoints;
    }
    
    void setPossiblePoints(int p) {
    	possiblePoints = p;
    }

    void addResourceLinkId(String r) {
    	resourceLinkIds.add(r);
    }
    
    boolean matchesResourceLinkId(String r) {
    	return resourceLinkIds.contains(r);
    }
    
    List<Key<Question>> getQuestionKeys() {
    	return questionKeys;
    }
    
    void addQuestionKey(Key<Question> k) {
    	questionKeys.add(k);
    }
    
    boolean removeQuestionKey(Key<Question> k) {
    	return questionKeys.remove(k);
    }
    
    public int compareTo(Assignment other) {
    	try {
    		return (int)(this.deadline.getTime() - other.deadline.getTime());
    	} catch (Exception e) {
    		return 0;
    	}
    }
}